package com.kyxx.topics.utils;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Environment;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.kyxx.topics.BaseApp;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.io.File;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

public class CommonUtils {

    private static final double EARTH_RADIUS = 6378137;// 地球半径

    /**
     * 动态设置ListView的高度
     *
     * @param listView
     */
    public static void setListViewHeightBasedOnChildren(ListView listView) {
        if (listView == null)
            return;

        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }

    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    /**
     * 根据两个经纬度计算距离
     *
     * @param lat1
     * @param lng1
     * @param lat2
     * @param lng2
     * @return
     */
    public static double GetDistance(double lat1, double lng1, double lat2, double lng2) {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lng1) - rad(lng2);

        double s = 2 * Math.asin(Math.sqrt(
                Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000;
        return s;
    }

    public static boolean CheckLogin() {
        boolean a = StringUtil.isEmpty(BaseApp.getModel().getId());
        return !StringUtil.isEmpty(BaseApp.getModel().getId());
    }

    public static void showPic(String name, ImageView imageview) {
        if (!StringUtil.isEmpty(name)) {
            ImageLoader.getInstance().displayImage(name, imageview);
        }
    }

    // 退出操作
    public static void exitkey(int keyCode, Context context) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {// 返回按钮
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setTitle("提示").setMessage("您确定退出当前应用")
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    try {
                        // 判断是否存在临时创建的文件
                        File temp_file = new File(
                                Environment.getExternalStorageDirectory() + File.separator + "heyi_dir");
                        Tools.Log("文件是否存在：" + temp_file.exists());
                        if (temp_file.exists()) {
                            File[] file_detail = temp_file.listFiles();
                            for (File file_del : file_detail) {
                                file_del.delete();
                            }
                            temp_file.delete();
                        }
                    } catch (Exception e) {
                    }
                    System.exit(0);
                }
            }).setOnCancelListener(new DialogInterface.OnCancelListener() {
                public void onCancel(DialogInterface dialog) {
                    dialog.dismiss();
                }

            }).show();
        }
    }

    public static void showShare(Context context, String title, String content, String url) {
        ShareSDK.initSDK(context);
        OnekeyShare oks = new OnekeyShare();
        // 关闭sso授权
        oks.disableSSOWhenAuthorize();
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        oks.setTitle(title);
        // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
        oks.setTitleUrl(url);
        // text是分享文本，所有平台都需要这个字段
        oks.setText(content);
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
//		oks.setImagePath("http://dashboard.mob.com/Uploads/db95c30283c2aa827e6831170d70808d.png");// 确保SDcard下面存在此张图片
        //网络图片
        oks.setImageUrl("http://www.mob.com/static/app/icon/1471335806.png");
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl(url);
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("content");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(title);
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl(url);
        // 启动分享GUI
        oks.show(context);
    }
}
